//
//  GHAddAreaViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/15.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHAddAreaViewController.h"

@interface GHAddAreaViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;
@property(nonatomic, strong) NSString                     *province;
@property(nonatomic, strong) NSString                     *city;
@end

@implementation GHAddAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加地址";
    [self st_setRightItemWithTitle:@"保存" titleColor:[UIColor whiteColor]];
    [self configDataSouce];
    // Do any additional setup after loading the view.
}
- (void)configDataSouce{
    
    NSString * name = self.addressModel.consignee;
    NSString * phone = self.addressModel.mobile;
    NSString * area = [NSString stringWithFormat:@"%@%@%@",self.addressModel.province,self.addressModel.city,self.addressModel.district];
    if (!self.addressModel) {
        area = @"";
    }
    NSString * detail = self.addressModel.address;
    self.dataSouce = @[
                       [[GHTableEasyModel alloc] initWithTextString:@"收货人" detailString:name],
                       [[GHTableEasyModel alloc] initWithTextString:@"手机号码" detailString:phone],
                       [[GHTableEasyModel alloc] initWithTextString:@"所在地区" detailString:area],
                       [[GHTableEasyModel alloc] initWithTextString:@"详细地址" detailString:detail],
                       ];
    
    
    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    GHTableEasyModel * model = self.dataSouce[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GHTableEasyModel * model = self.dataSouce[indexPath.row];
    if (indexPath.section == 0 && indexPath.row == 0) {
        [self onSelectedNameCellWithModel:model];
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        [self onSelectedPhoneCellWithModel:model];
    }
    //收货地址管理
    if (indexPath.section == 0 && indexPath.row == 2) {
        [self onSelectedAreaCellWithModel:model];
    }
    
    if (indexPath.section == 0 && indexPath.row == 3) {
        [self onSelectedDetailCellWithModel:model];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method


//收货人
- (void)onSelectedNameCellWithModel:(GHTableEasyModel*)model{
    STTableViewDetailController * detail =
    [[STTableViewDetailController alloc] initWithPlaceholder:@"请输入收货人"
                                                       title:@"收货人"
                                                        text:model.detailString
                                                    complete:^(NSString *text, STTableViewDetailController *detailControllerVC) {
                                                        model.detailString = text;
                                                        [self.tableView reloadData];
                                                        [self.navigationController popViewControllerAnimated:YES];
                                                    }];
    detail.detailLabel.text = @"";
    detail.isShowTextNum = NO;
    [self.navigationController pushViewController:detail animated:YES];
}
//手机号码
- (void)onSelectedPhoneCellWithModel:(GHTableEasyModel*)model{
    STTableViewDetailController * detail =
    [[STTableViewDetailController alloc] initWithPlaceholder:@"请输入手机号码"
                                                       title:@"手机号码"
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadData];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    
    detail.isShowTextNum = NO;
    detail.textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)onSelectedAreaCellWithModel:(GHTableEasyModel*)model{
    
    STPickerViewController * picker = [[STPickerViewController alloc] initWithDefualtAreaPickerWithHandle:^(NSString *finshString, NSString *area, NSString *city, NSString *provence) {
        model.detailString = [NSString stringWithFormat:@"%@%@",provence,city];
        self.province = provence;
        self.city = city;
        [self.tableView reloadData];
    }];
    [self presentViewController:picker animated:NO completion:nil];
}
- (void)onSelectedDetailCellWithModel:(GHTableEasyModel*)model{
    STTableViewDetailController * detail =
    [[STTableViewDetailController alloc] initWithPlaceholder:@"请输入详细地址"
                                                       title:@"详细地址"
                                                        text:model.detailString
                                                      handle:^(NSString *text) {
                                                          model.detailString = text;
                                                          [self.tableView reloadData];
                                                          [self.navigationController popViewControllerAnimated:YES];
                                                      }];
    
    detail.isShowTextNum = NO;
    detail.textFiled.keyboardType = UIKeyboardTypeDefault;
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)st_rightBarAction:(id)sender{
    
    [self sendAddressRequestWithId:self.addressModel.addressId];
    
}
#pragma mark --NetWork Method
//传入id 则为编辑请求，不传，新增地址请求
- (void)sendAddressRequestWithId:(NSString*)address_id{
    [SVProgressHUD showSuccessWithStatus:@"提交中"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    GHTableEasyModel * peopleModel = self.dataSouce.firstObject;
    if (!peopleModel.detailString.length) {
        [SVProgressHUD showErrorWithStatus:@"收件人不能为空"];
        return;
    }else{
        [dic setObject:peopleModel.detailString forKey:@"consignee"];
    }
    GHTableEasyModel * phoneModel = self.dataSouce[1];
    if (!phoneModel.detailString.length) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return;
    }else{
        [dic setObject:phoneModel.detailString forKey:@"mobile"];
    }
    GHTableEasyModel * areaModel = self.dataSouce[2];
    if (!areaModel.detailString.length) {
        [SVProgressHUD showErrorWithStatus:@"所在地区不能为空"];
        return;
    }else{
        if (self.province) {
            [dic setObject:self.province forKey:@"province"];
        }
        if (self.city) {
            [dic setObject:self.city forKey:@"city"];
        }
        [dic setObject:@" " forKey:@"district"];
    }
    GHTableEasyModel * detailModel = self.dataSouce.lastObject;
    if (!detailModel.detailString.length) {
        [SVProgressHUD showErrorWithStatus:@"详细地址不能为空"];
        return;
    }else{
        [dic setObject:detailModel.detailString forKey:@"address"];
    }

    NSString *addUrl = @"http://test.smqyp.com/Api/goods/save_addr";
    NSString *editUrl = @"http://test.smqyp.com/Api/system/edit_receive_addr";
    NSString *url = addUrl;
    if (address_id.length) {
        [dic setObject:address_id forKey:@"addr_id"];
        url = editUrl;
    }
    
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 if (address_id.length) {
                                                      [SVProgressHUD showSuccessWithStatus:@"更改地址成功"];
                                                 }else{
                                                      [SVProgressHUD showSuccessWithStatus:@"添加地址成功"];
                                                 }
                                                
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

